package com.bbm.bbmanage.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bbm.bbmanage.entity.AppHttpCodeEnum;
import com.bbm.bbmanage.entity.HouseOrder;
import com.bbm.bbmanage.entity.Reserve;
import com.bbm.bbmanage.entity.vo.PageVo;
import com.bbm.bbmanage.entity.vo.ResponseResult;
import com.bbm.bbmanage.mapper.HouseOrderMapper;
import com.bbm.bbmanage.service.HouseOrderService;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * (HouseOrder)表服务实现类
 *
 * @author lionkliu
 * @since 2024-04-17 14:14:44
 */
@Service("houseOrderService")
public class HouseOrderServiceImpl extends ServiceImpl<HouseOrderMapper, HouseOrder> implements HouseOrderService {
    
    @Override
    public ResponseResult selectPage(Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<HouseOrder> queryWrapper = new LambdaQueryWrapper<>();
        Page<HouseOrder> page = page(new Page<>(pageNum, pageSize), queryWrapper);
        PageVo<HouseOrder> pageVo = new PageVo<>();
        pageVo.setResult(page.getRecords());
        pageVo.setTotal(page.getTotal());
        pageVo.setPageTotal(pageVo.getPageTotal(pageSize));
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult selectOne(Integer id) {
        return ResponseResult.okResult(getById(id));
    }

    @Override
    public ResponseResult insertOne(HouseOrder houseOrder) {
        return save(houseOrder) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.INSERT_ERROR);
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        return removeById(id) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.DELETE_ERROR);
    }

    @Override
    public ResponseResult updateOne(HouseOrder houseOrder) {
        LambdaUpdateWrapper<HouseOrder> updateWrapper = new LambdaUpdateWrapper();
        updateWrapper.eq(HouseOrder::getId, houseOrder. getId());
        // TODO
        update(null, updateWrapper);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult search(Integer pageNum, Integer pageSize, Integer userId) {
        LambdaQueryWrapper<HouseOrder> queryWrapper = new LambdaQueryWrapper<>();
        if (!Objects.isNull(userId)) {
            queryWrapper.eq(HouseOrder::getUserId, userId);
        }
        Page<HouseOrder> page = new Page<>(pageNum, pageSize);
        page(page, queryWrapper);
        PageVo<HouseOrder> pageVo = new PageVo<>();
        pageVo.setTotal(page.getTotal());
        pageVo.setPageTotal(page.getPages());
        pageVo.setResult(page.getRecords());

        return ResponseResult.okResult(pageVo);
    }
}

